package com.liuguang.gateway;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.digest.BCrypt;
import cn.hutool.crypto.digest.MD5;
import com.liuguang.common.constans.CommonConstans;
import com.liuguang.common.utils.EncryForRSAUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ClassUtils;
import org.springframework.web.servlet.DispatcherServlet;

import java.io.*;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @ClassName: ClassFound
 * @Description：
 * @Author: wanghr
 * Date: 10:30 AM 2020/9/13
 * Version: 1.0
 */
public class ClassFound {
    public static void main(String[] args) {
//        SecurityContextPersistenceFilter
//        DispatcherServlet
//        dispatcherServlet();
//        a();
//        b();
//        RSATest();
//        file();
//        base64();
//        MD5();
//        Bcrypt();
//        dateFormate();

    }

    private static void dateFormate() {
        // date 转 字符串 一
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("现在日期"+"yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        System.out.println("format = " + format);

        // date 转 字符串 二
        DateFormat instance = DateFormat.getInstance();
        String format1 = instance.format(new Date());
        System.out.println("format1 = " + format1);


        // 字符串 转 date 时间
        try {
            Date parse = simpleDateFormat.parse(format);
            System.out.println("parse = " + parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private static void Bcrypt() {
        String hashpw = BCrypt.hashpw("1233w");
        System.out.println("hashpw = " + hashpw);

        String hashpw1 = BCrypt.hashpw("1233w");
        System.out.println("hashpw1 = " + hashpw1);
    //hashpw = $2a$10$N3vfQUWzc4mG5PxbNQYTDO9EVxCWHjqiPUpAjMmhhmZJxnwoDArhe
    //hashpw1 = $2a$10$gifnyCuqc.Ka.UUx3bWd6.7YEEAvBz/lsjuM4STDRnKVDKjqIXfLC
    }

    private static void MD5() {
        MD5 md5 = MD5.create();
        String digestHex = md5.digestHex("123www");
        System.out.println("digestHex = " + digestHex);

        String digestHex1 = md5.digestHex("123www");
        System.out.println("digestHex1 = " + digestHex1);
    }

    private static void base64() {
        String encode = Base64Encoder.encode("1234ws");
        String encode1 = Base64Encoder.encode("1234ws");
        System.out.println("encode1 = " + encode1);
        System.out.println("encode = " + encode);
        byte[] decode = Base64Decoder.decode(encode);
        System.out.println("decode = " + decode);
    }

    private static void file() {
        try {
            ClassPathResource resource = new ClassPathResource("/keys/privatekey.txt");
            File resourceFile = resource.getFile();

            boolean exists = resourceFile.exists();
            System.out.println("exists = " + exists);
            if (exists) {
//                inputOutputStream(resourceFile);
                BufferedReader bufferedReader = new BufferedReader(new FileReader(resourceFile));
                StringBuffer stringBuffer = new StringBuffer();
                String s;
                while ((s = bufferedReader.readLine()) != null) {
                    stringBuffer.append(s);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        System.out.println("path = " + path);
//        new File()
    }

    private static void inputOutputStream(File resourceFile) throws IOException {
        FileInputStream inputStream = new FileInputStream(resourceFile);
        int available = inputStream.available();
        byte[] buffer = new byte[available];
        inputStream.read(buffer);
        String s = new String(buffer);
        System.out.println("s = " + s);
    }

    private static void RSATest() {
        RSA rsa = SecureUtil.rsa();
        String publicKeyBase64 = rsa.getPublicKeyBase64();
        System.out.println("publicKeyBase64 = " + publicKeyBase64);

        String privateKeyBase64 = rsa.getPrivateKeyBase64();
        System.out.println("privateKeyBase64 = " + privateKeyBase64);


        //模拟前端使用公钥加密
        byte[] bytes = rsa.encrypt("wdsf王", KeyType.PublicKey);

        //todo: 前端得到公钥后进行加密，加密以什么形式传给后端？如果是 String 那么String形式

        //后端使用私钥解密   生成一对密钥就行，所有的公用，没必要每次都生成
        //私钥保存 MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsTtF4dh5yzM/9DPBenHcp
//                MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0p0fQghiPUA1jNeEZXA6En
        //解密
        RSA newRsa = SecureUtil.rsa(privateKeyBase64, publicKeyBase64);
        byte[] decrypt = newRsa.decrypt(bytes, KeyType.PrivateKey);
        String s = new String(decrypt);
        System.out.println("s = " + s);
    }

    private static void b() {
        HashMap<String, Object> keys = null;
        try {
            keys = EncryForRSAUtil.getKeys();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        RSAPrivateKey privateKey = (RSAPrivateKey) keys.get(CommonConstans.PRIVATE_KEY);
        String s = privateKey.getModulus().toString();
        System.out.println("s = " + s);
        String s1 = privateKey.getModulus().toString(16);
        System.out.println("s1 = " + s1);
        int length = s.length();
        System.out.println("length = " + length);
        int length1 = s1.length();
        System.out.println("length1 = " + length1);
    }

    private static void a() {
        HashMap<String, Object> keys = null;
        try {
            keys = EncryForRSAUtil.getKeys();
            RSAPublicKey publicKey = (RSAPublicKey) keys.get(CommonConstans.PUBLIC_KEY);
            BigInteger publicExponent = publicKey.getPublicExponent();
            System.out.println("publicExponent = " + publicExponent);
            BigInteger modulus = publicKey.getModulus();
            System.out.println("modulus = " + modulus);

            RSAPrivateKey privateKey = (RSAPrivateKey) keys.get(CommonConstans.PRIVATE_KEY);
            BigInteger privateExponent = privateKey.getPrivateExponent();
            System.out.println("privateExponent = " + privateExponent);
            BigInteger modulus1 = privateKey.getModulus();
            System.out.println("modulus1 = " + modulus1);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    private static void dispatcherServlet() {
        RSA rsa = SecureUtil.rsa();
        byte[] encrypt = rsa.encrypt("wowang".getBytes(), KeyType.PublicKey);
        System.out.println("encrypt = " + encrypt);
        String s = null;
        try {
            s = new String(encrypt, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        System.out.println("s = " + s);
//
        byte[] bytes = rsa.decrypt(encrypt, KeyType.PrivateKey);
        String s1 = new String(bytes);
        System.out.println("s1 = " + s1);

//        RSAPublicKeyImpl publicKey = (RSAPublicKeyImpl)rsa.getPublicKey();
//        BigInteger modulus = publicKey.getModulus();
//-----------------------------------------------
//        RSA rsa = SecureUtil.rsa();
//
//        String s = new String(rsa.encrypt("woasd".getBytes(), KeyType.PublicKey));
//        System.out.println("s = " + s);
//        String decrypt = new String(rsa.decrypt(s.getBytes(), KeyType.PrivateKey));
//        System.out.println("decrypt = " + decrypt);
//
//        String publicKeyBase64 = rsa.getPublicKeyBase64();
//        System.out.println("publicKeyBase64 = " + publicKeyBase64);
//
////        PrivateKey privateKey1 = rsa.getPrivateKey();
////        System.out.println("privateKey1 = " + privateKey1);
//
//        String privateKeyBase64 = rsa.getPrivateKeyBase64();
//        System.out.println("privateKeyBase64 = " + privateKeyBase64);
//
//
////        PrivateKey privateKey = SecureUtil.generatePrivateKey("RSA", privateKeyBase64.getBytes());
////        System.out.println("privateKey = " + privateKey);
//
//        RSA rsa1 = SecureUtil.rsa(privateKeyBase64, publicKeyBase64);
//        byte[] encrypt = rsa1.decrypt(s.getBytes(), KeyType.PrivateKey);
//        System.out.println("encrypt = " + new String(encrypt));
    }
}
